<template>
	<view class="page">
		<view class="user">
			<image class="avatar" src="/static/center2/avatar.png"></image>
			<view class="name">
				<text>{{username}}</text>
				<text>重复的工作交给我</text>
			</view>
		</view>
		<view class="card" v-for="(v, k) in menus" :key="k">
			<view class="menu">
				<view class="item" v-for="(v_, k_) in v" :key="k_" @click="menuClick(v_)">
					<image :src="v_.icon"></image>
					<view>{{v_.name}}</view>
					<image class="arrow" v-if="v_.arrow" src="/static/center2/right.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { onMounted, computed, ref } from 'vue';
	import { useStore } from 'vuex';
	import { userKey } from '../../src/store';
	import { onShow } from '@dcloudio/uni-app';

	const userInfo = useStore(userKey);
	const username = ref('');
	const menus = ref([
		[{
			name: '我的书籍',
			icon: '/static/center2/book.png',
			arrow: true
		},
		{
			name: '我的账户',
			icon: '/static/center2/self.png',
			arrow: true
		},
		{
			name: '图书捐赠',
			icon: '/static/center2/heart.png',
			arrow: true
		},
		],
		[{
			name: '联系作者',
			icon: '/static/center2/kefu.png',
			arrow: true
		},
		{
			name: '退出登录',
			icon: '/static/center2/out.png',
			arrow: false
		},
		]
	]);

	onShow(() => {
		username.value = userInfo.state.username
	})
	const loginOut = () => {
		userInfo.commit("clearUserInfo");
		username.value=userInfo.state.username;
		console.log('清除登录信息');
		uni.switchTab({
			url: '/pages/index/index'
		})
	}
	const menuClick = (e) => {
		console.log(e);
		switch (e.name) {
			case '退出登录':
				loginOut();
				break;
		}
	}
</script>

<style lang="scss">
	.page {
		background: #f5f5f5;
		min-height: 100vh;

		.action {
			display: flex;
			justify-content: flex-end;
			padding: 10rpx;
			background: #22ac38;

			view {
				padding: 10rpx;
				margin: 0 10rpx;
				border-radius: 8rpx;
				width: 45rpx;
				height: 45rpx;

				&:active {
					background: #ccc;
				}

				image {
					width: 45rpx;
					height: 45rpx;
				}
			}
		}

		.user {
			display: flex;
			align-items: center;
			padding: 25rpx;
			background: #4B79EC;

			&:active {
				background: #f0f0f0;
			}

			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 12rpx;
				border: 3px solid #3a5fb6;
			}

			.right {
				width: 30rpx;
				height: 30rpx;
			}

			.name {
				flex-grow: 1;
				display: flex;
				flex-direction: column;
				padding-left: 20rpx;

				text {
					color: #fff;

					&:nth-child(1) {
						font-size: 16px;
						font-weight: bold;
					}

					&:nth-child(2) {
						font-size: 12px;
						color: #f0f0f0;
					}
				}
			}
		}

		.card {
			background: #fff;
			box-sizing: border-box;
			width: 700rpx;
			margin: auto;
			padding: 15rpx;
			margin-top: 20rpx;
			border-radius: 12rpx;

			.menu {

				.item {
					display: flex;
					height: 100rpx;
					align-items: center;
					padding: 0 20rpx;
					box-sizing: border-box;
					width: 700rpx;

					&:active {
						background: #f0f0f0;
					}

					image {
						height: 45rpx;
						width: 45rpx;
					}

					view {
						font-size: 14px;
						color: #444;
						padding-left: 15rpx;
						box-sizing: border-box;
						width: calc(100% - 100rpx);
					}

					.arrow {
						width: 26rpx;
						height: 26rpx;
					}
				}
			}
		}

	}
</style>